package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.service.IOaYcSysUserService;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaQxjsp;
import com.yuncheng.oaApi.service.IOaQxjspService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 请休假审批表
 * @Author: yuncheng
 * @Date: 2023-09-08
 * @Version: V1.0
 */
@Api(tags = "请休假审批表接口")
@RestController
@RequestMapping("/api/spzx-oa/oaQxjsp")
public class OaQxjspController extends BaseController<OaQxjsp, IOaQxjspService> {

    private static final Logger log = LoggerFactory.getLogger(OaQxjspController.class);

    @Resource
    private IOaQxjspService oaQxjspService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;
    /**
     * 分页列表查询
     *
     * @param oaQxjsp
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "请休假审批表-分页列表查询")
    @ApiOperation(value = "请休假审批表-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaQxjsp oaQxjsp, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaQxjsp> queryWrapper = QueryBuilder.initQueryWrapper(oaQxjsp, req.getParameterMap());
        Page<OaQxjsp> page = new Page<>(pageNo, pageSize);
        IPage<OaQxjsp> pageList = oaQxjspService.page(page, queryWrapper);
        for (OaQxjsp vo : pageList.getRecords()) {
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }


    /**
     * 添加
     *
     * @param oaQxjsp
     * @return
     */
    @AutoLog(value = "流程业务主表-添加")
    @ApiOperation(value = "流程业务主表-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaQxjsp oaQxjsp) {
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(oaQxjsp.getId())){
            oaQxjsp.setId(GetUuIdUtils.ReplaceUuId());
        }
        if (StringUtils.isBlank(oaQxjsp.getFxm())){
            oaQxjsp.setFxm(currentUser.getName());
        }
        if (StringUtils.isBlank(oaQxjsp.getFbm())){
            oaQxjsp.setFbm(currentUser.getDeptName());
        }
        if (StringUtils.isBlank(oaQxjsp.getFzw())){
            oaQxjsp.setFzw(ycSysUserService.getById(currentUser.getId()).getFzw());
        }
        if (StringUtils.isBlank(oaQxjsp.getFuserid())){
            oaQxjsp.setFuserid(currentUser.getId());
        }

        oaQxjspService.save(oaQxjsp);
        return HttpResult.ok(oaQxjsp);
    }



    /**
     * 编辑
     *
     * @param oaQxjsp
     * @return
     */
    @AutoLog(value = "请休假审批表-编辑")
    @ApiOperation(value = "请休假审批表-编辑")
    //@RequiresPermissions("qsj:OaQxjsp:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaQxjsp oaQxjsp) {
        oaQxjspService.updateById(oaQxjsp);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }


    @AutoLog(value = "导出请休假数据-表格excel")
    @ApiOperation(value = "导出请休假数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportqxjData")
    public void exportqxjData(@RequestParam(value = "ids",defaultValue = "")String ids,
                              OaQxjsp ndkpBasic,
                               HttpServletResponse response){
        try {
            String name = "exportqxjData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaQxjsp> ndkpBasicList = oaQxjspService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaQxjsp e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaQxjsp> queryWrapper = new LambdaQueryWrapper<>();
                //部门
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFbm()),OaQxjsp::getFbm, ndkpBasic.getFbm());
                //姓名
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFxm()),OaQxjsp::getFxm,ndkpBasic.getFxm());
                //请假类型
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFqjlx()),OaQxjsp::getFqjlx,ndkpBasic.getFqjlx());
                //请假开始时间
                if (ndkpBasic.getFqjkssj() != null){
                    queryWrapper.ge(OaQxjsp::getFqjkssj,ndkpBasic.getFqjkssj());
                }
                //请假结束时间
                if (ndkpBasic.getFqjjssj() != null){
                    queryWrapper.le(OaQxjsp::getFqjjssj,ndkpBasic.getFqjjssj());
                }
                List<OaQxjsp> ndkpBasicList = oaQxjspService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaQxjsp e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_QXJSP_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出行请休假审批数据出错-{}",e.getMessage());
        }
    }


    /**
     * 数据转换
     *
     * @param oaQxjsp
     */
    private void formatValue(OaQxjsp oaQxjsp) {
    }
}
